class Area
  attr_reader :db_id, :num, :texture, :coord_x, :coord_y, :start, :min_level, :max_level
  
  @@list    ||= nil
  @@current ||= nil
  
  ##### ACCESSORS #####
  def self.[](index); @@list[index] end
  
  def self.all; @@list end
  def self.current; @@current end
  def self.set_current(index); @@current = @@list[index] end
  
  ##### CHECKS #####
  def self.exist?(index) @@list.key?(index) end
  def self.count; @@list.length end
  def self.unexplored(id = nil)
    return $db.execute("
      SELECT r.id, r.name, group_concat(e.cmd)
      FROM Exit e
      JOIN ROOM r ON r.id = e.from_id
      WHERE r.area_id = ?
        AND to_id NOT IN (SELECT id FROM Room)
      GROUP BY r.id, r.name
      ORDER BY r.id", id.nil? ? Area.current.db_id : id
    )
  end
  
  ##### INITIALIZATION #####
  def self.init(id)
    @@list = {} if @@list.nil?
    area_row = $db.get_first_row("
      SELECT id, num, name, coord_x, coord_y, start, min_level, max_level
      FROM Area WHERE id = ?", id
    )
    Area.new(area_row)
  end
  
  def initialize(area_row)
    @db_id, @num, @texture, @coord_x, @coord_y, @start, @min_level, @max_level = area_row
    @rooms = []
    @@list[@num], @@list[@db_id] = self, self
  end
end